package com.doitedu;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.util.List;

/**
 * @Date 2021/12/10
 * @Created by HANGGE
 * @Description
 * 更新数据
 */
public class C05_Delete {
    public static void main(String[] args) throws Exception {
        ZooKeeper zk = new ZooKeeper("linux01:2181,linux02:2181,linux03:2181", 2000, null);
        // 判断节点是否存在  如果存在 返回 Stat  不为null
        Stat exists = zk.exists("/a", null);
        if(exists!=null){
            rmr("/a" ,zk);
        }
        zk.close();
    }
    //   /a/a1
    public static  void rmr(String path , ZooKeeper zk) throws Exception {

        List<String> ls = zk.getChildren(path, null);
        // 判断是否有子节点
        if(ls!=null && ls.size()>0){
            for (String l : ls) {
                rmr(path+"/"+l ,zk);
            }
        }
        zk.delete(path, -1);
    }
}
